package com.macken.wowadmin.dao;

import java.util.List;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.macken.wowadmin.entity.AdminInfo;
import com.macken.wowadmin.util.DaoSupport;

public class AdminDao {
	public static final AdminDao instance = new AdminDao();
	private AdminDao() {

	}

	public List<AdminInfo> getAllAdminList() {
		String sql = "select * from admin_info";
		return DaoSupport.db.query(sql, AdminInfo.rowMapper);
	}

	public AdminInfo getAdminById(int adminId) {
		String sql = "select * from admin_info where admin_id =?";
		List<AdminInfo> list = DaoSupport.db.query(sql, AdminInfo.rowMapper, adminId);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	public AdminInfo getAdminByHostId(int hostid){
		String sql="select * from admin_info where host_id=?";
		List<AdminInfo> list=DaoSupport.db.query(sql, AdminInfo.rowMapper, hostid);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public boolean add(AdminInfo admin) {
		String sql = "insert into admin_info (host_id,admin_name,admin_pass,note)values(:hostId,:adminName,:adminPass,:note)";
		int rows = DaoSupport.db.getNamedParameterJdbcOperations().update(sql,
				new BeanPropertySqlParameterSource(admin));
		return rows > 0;
	}

	public boolean update(AdminInfo admin) {
		String sql = "update admin_info set host_id=?,admin_name=?,admin_pass=?,note=? where admin_id=?";
		int rows = DaoSupport.db.update(sql, admin.getHostId(), admin.getAdminName(), admin.getAdminPass(),
				admin.getNote(), admin.getAdminId());
		return rows > 0;
	}
	public boolean del(int adminId) {
		String sql = "delete from admin_info where admin_id=?";
		int rows = DaoSupport.db.update(sql, adminId);
		return rows > 0;
	}
}
